﻿// -----------------------------
// Copyright © Kodi Studios 2012
// -----------------------------

using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Kodi.Picturethrill
{
    public class NationalGeographicImageProvider : IImageProvider
    {
        private const string NationalGeographicUrl = "http://photography.nationalgeographic.com/photography/photo-of-the-day";
        private List<string> imageUrls = new List<string>();

        public NationalGeographicImageProvider()
        {
        }

        public IList<string> ImageUrls
        {
            get
            {
                if (this.imageUrls.Count == 0)
                {
                    string imageUrl;

                    // Try to get highest resolution images
                    // ExampleMatch: 
                    // <div class="download_link"><a href="http://images.nationalgeographic.com/wpf/media-live/photos/000/548/custom/54822_1600x1200-wallpaper-cb1340033459.jpg">Download Wallpaper (1600 x 1200 pixels)</a></div>
                    //                                                                      <div class="  download_link"  > < a href="  http://im                    
                    imageUrl = HtmlUtilities.GetImageUrl(NationalGeographicUrl, new Regex(@"<div class=\""download_link\""\>\<a href=\""([^\""]+)"));

                    if (string.IsNullOrEmpty(imageUrl))
                    {
                        // Sometimes, high resolution images are not available, fall back to standard resolution

                        // ExampleMatch: 
                        // <div class="primary_photo">
                        //
                        //    <a href="/photography/photo-of-the-day/himba-woman-namibia/" title="Go to the previous Photo of the Day">
                        //      <img src="http://images.nationalgeographic.com/wpf/media-live/photos/000/447/cache/spider-web-cocooons-pakistan_44718_990x742.jpg" width="990" height="742" alt="Photo: Trees cocooned in spiderwebs">
                        imageUrl = HtmlUtilities.GetImageUrl(NationalGeographicUrl, new Regex(@"\<div class\=\""primary_photo""[^\<]+\<[^\<]+\<img src=\""([^\""]+)"));
                    }

                    imageUrls.Add(imageUrl);
                }
                return this.imageUrls;
            }
        }

        public string Name
        {
            get
            {
                return "NationalGeographic";
            }
        }

        public string SiteName
        {
            get
            {
                return NationalGeographicUrl;
            }
        }
    }
}
